//复写零
class Solution {
public:
    void moveZeroes(vector<int>& nums) 
    {
        int n = nums.size();
        int dest = -1;
        int cur = 0;
        while(cur < n)
        {
            if(nums[cur] == 0)
                cur++;
            else
            {
                swap(nums[cur],nums[dest+1]);
                cur++;
                dest++;
            }
        }
    }
};

//三数之和
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> ret;
        int n = nums.size();
        
        //排序
        sort(nums.begin(),nums.end());

        for(int i = 0; i < n;)
        {
            if(nums[i] > 0)//当固定的数大于0，不需要再找了
                break;

            int left = i + 1, right = n - 1, target = -nums[i];
            while(left < right)
            {
                int sum = nums[left] + nums[right];
                if(sum > target) 
                    right--;
                else if(sum < target)
                    left++;
                else
                {
                //将结果保存到容器后，将移动left和right，此时需要对left和right去重
                    ret.push_back({nums[i], nums[left], nums[right]});
                    left++;
                    right--;
                
                //去重
                while(left < n && nums[left] == nums[left-1])
                    left++;
                while(right > i && nums[right] == nums[right+1]) 
                    right--;
                }
            }
            i++;
            //去重i
            while(i < n && nums[i] == nums[i-1])
                i++;
        }
        return ret;
    }
};

class Solution {
public:
    //  利用快慢指针的思想

    //求出每位数的平方和
    int sum(int n)
    {
        int ret = 0;
        while(n)
        {
            int tmp = n % 10;
            ret = ret + tmp*tmp;
            n /= 10;
        }
        return ret;
    }

    bool isHappy(int n) 
    {
        int slow = n, fast = sum(n);
        while(slow != fast)
        {
            slow = sum(slow);
            fast = sum(sum(fast));
        }

        return slow == 1;
    }
};